<?php

include_once('Database.php');
include_once('RestUtils.php');
include_once('model/Passenger.php');

/**
 * Gets the all information for a passenger in JSON format
 *
 * $passengerId	Id of the passenger to get information about
 */
function getPassengerInformation($passengerId)
{
	$db = new DBManager();
	$mysqli = $db->db_connect();

	$query = "SELECT * FROM Passenger WHERE passengerId=?";

	$stmt = $mysqli->prepare($query);
	$stmt->bind_param('i', $passengerId);
	$stmt->execute();
	$result = $stmt->get_result();	

	$row = $result->fetch_assoc();

	$passenger = new Passenger($row['passengerId'], $row['creationTime'], $row['boardTime'], $row['unboardTime'], $row['vehicleId'], $row['destinationStationId']);

	return json_encode($passenger);
}

/* Initialize the request
 *
 * Request format: PATH/Passengers.php/passengerId
 *
 * $requestData->getRequestVars() will have an array such that
 *
 * [3] will be passengerId
 *
 * For more details about $requestData, see RestRequest class in RestUtils.php */
$requestData = RestUtils::processRequest();

/* Now let's handle the request */
switch($requestData->getMethod())
{
	/* If it was a GET request */
	case 'get':
		/* If there is a passengerId */
		if(isset($requestData->getRequestVars()[4]))
		{
			/* At this point the all information is wanted for a specific passenger */
			$result = getPassengerInformation($requestData->getRequestVars()[4]);
		}
		else
		{
			/* A passengerId should be provided, bad request */
			echo RestUtils::sendResponse(400, 'Bad request! You should provide a passengerId.');

			break;
		}

		/* Echo the result */
		echo RestUtils::sendResponse(200, $result, 'application/json');
		break;

	/* No need to implement POST, PUT and DELETE requests yet */
}

?>